<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Base\Annotation\Common;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ODM\Document(repositoryClass="App\Repository\GiftRepository")
 */
class Gift
{
    /**
     * @Common(cnName="#",editType="text",listType="text")
     * @ODM\Id(strategy="INCREMENT")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"gift"})
     */
    private $id;

    /**
     * @Common(cnName="名称",editType="text",listType="text")
     * @ODM\Field( nullable=true)
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"gift"})
     */
    private $name;

    /**
     * @Common(cnName="图片",editType="pic",listType="text")
     * @ODM\Field( nullable=true)
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"gift"})
     */
    private $pic;

    /**
     * @Common(cnName="积分",editType="text",listType="text")
     * @ODM\Field(type="raw", nullable=true)
     * @ORM\Column(type="float", nullable=true)
     * @Groups({"gift"})
     */
    private $price;

    /**
     * @Common(cnName="是否上架",editType="switch",listType="text")
     * @ODM\Field(type="boolean", nullable=true)
     * @ORM\Column(type="boolean")
     * @Groups({"gift"})
     */
    private $isOn;

    /**
     * @Common(cnName="排序",editType="text",listType="text")
     * @ODM\Field(type="raw", nullable=true)
     * @ORM\Column(type="integer", nullable=true)
     * @Groups({"gift"})
     */
    private $sort;

    /**
     * @ODM\ReferenceMany(targetDocument="App\Entity\GiftLog", mappedBy="gift", orphanRemoval=true)
     * @ORM\OneToMany(targetEntity="App\Entity\GiftLog", mappedBy="gift", orphanRemoval=true)
     */
    private $logs;

    public function __construct()
    {
        $this->logs = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getPic(): ?string
    {
        return $this->pic;
    }

    public function setPic(?string $pic): self
    {
        $this->pic = $pic;

        return $this;
    }

    public function getPrice(): ?float
    {
        return $this->price;
    }

    public function setPrice(?float $price): self
    {
        $this->price = $price;

        return $this;
    }

    public function getIsOn(): ?bool
    {
        return $this->isOn;
    }

    public function setIsOn(bool $isOn): self
    {
        $this->isOn = $isOn;

        return $this;
    }

    public function getSort(): ?int
    {
        return $this->sort;
    }

    public function setSort(?int $sort): self
    {
        $this->sort = $sort;

        return $this;
    }

    /**
     * @return Collection|GiftLog[]
     */
    public function getLogs(): Collection
    {
        return $this->logs;
    }

    public function addLog(GiftLog $log): self
    {
        if (!$this->logs->contains($log)) {
            $this->logs[] = $log;
            $log->setGift($this);
        }

        return $this;
    }

    public function removeLog(GiftLog $log): self
    {
        if ($this->logs->contains($log)) {
            $this->logs->removeElement($log);
            // set the owning side to null (unless already changed)
            if ($log->getGift() === $this) {
                $log->setGift(null);
            }
        }

        return $this;
    }
}
